package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.vo.CourseInfoVo;
import com.atguigu.serviceedu.entity.vo.CoursePublishVo;
import com.atguigu.serviceedu.entity.vo.CourseQuery;
import com.atguigu.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author jyd
 * @since 2022-03-20
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;
    //添加课程信息和简介
    @PostMapping("AddCourse")
    public R AddCourse(@RequestBody CourseInfoVo courseInfoVo){
        String cid = eduCourseService.saveCourse(courseInfoVo);
        if(StringUtils.isNullOrEmpty(cid)){
            return R.error().message("保存失败");
        }else {
            return R.ok().data("courseId",cid);
        }
    }
    //根据id查询课程信息和简介
    @GetMapping("getCourseById/{courseId}")
    public R getCourseById(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.getCourseById(courseId);
        return R.ok().data("courseInfo",courseInfoVo);
    }
    //修改信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //获取课程发布信息
    @ApiOperation(value = "课程发布信息")
    @GetMapping("getCoursePublishVo/{courseId}")
    public R getCoursePublishVo(@PathVariable String courseId){
        CoursePublishVo coursePublishVo = eduCourseService.getCoursePublishVo(courseId);
        return R.ok().data("publishCourse",coursePublishVo);
    }
    //课程列表条件查询分页
    @ApiOperation(value = "课程列表条件查询分页")
    @PostMapping("PageCourseCondition/{current}/{limit}")
    public R PageCourseCondition(@PathVariable Long current, @PathVariable Long limit,
                                 @ApiParam(name = "courseQuery", value = "查询对象", required = false)@RequestBody CourseQuery courseQuery){
        Page<EduCourse> coursePage = new Page<>(current,limit);
        eduCourseService.selectCourse(coursePage,courseQuery);
        long total = coursePage.getTotal();
        List<EduCourse> records = coursePage.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }
    //删除课程
    @ApiOperation(value = "课程列表删除")
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        eduCourseService.removeCourse(courseId);
        return R.ok();
    }
}

